const router = require('express').Router({

});
const { check, validationResult } = require('express-validator'); //数据验证
const specializedControllers = require("../../controllers/specializedControllers");
/* GET */
router.get(
  '/',
  [
    check('pageNo').notEmpty().withMessage('页码不能为空'),
    check('pageSize').notEmpty().withMessage('条数不能为空')
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }
    let data = await specializedControllers.selectList(req.query.pageNo, req.query.pageSize, req.query.major, req.query.department);

    res.sendResult(data, 200, "请求成功");

  }
);

router.post(
  '/',
  [
    check('major_name').notEmpty().withMessage('major_name参数不能为空'),
    check('major_department_id').notEmpty().withMessage('major_department_id参数不能为空')
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data1 = await specializedControllers.selectByNameAndCondition(req.body.major_name, null, "0");

    if (data1) {
      res.sendResult(null, 400, "添加失败,专业名已存在");
      return;
    }
    let data2 = await specializedControllers.selectByNameAndCondition(req.body.major_name, null, "1");

    if (data2) {
      await specializedControllers.updateByDid(req.body.major_name, req.body.major_department_id,data2.major_id, 0)
    } else {
      await specializedControllers.insert(req.body.major_name, req.body.major_department_id)
    }
    res.sendResult({}, 201, "添加成功");
  }
);

router.put(
  '/',
  [
    check('major_id').notEmpty().withMessage('major_id参数不能为空'),
    check('major_name').notEmpty().withMessage('major_name参数不能为空'),
    check('major_department_id').notEmpty().withMessage('major_department_id参数不能为空')
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data1 = await specializedControllers.selectById(req.body.major_id);
    if (!data1) {
      res.sendResult(null, 400, "更新失败,major_id不存在");
      return;
    }

    let data2 = await specializedControllers.selectByNameAndCondition(req.body.major_name, req.body.major_id);
    if (data2) {
      res.sendResult(null, 400, "更新失败,专业名已存在");
      return;
    }

    let flag = await specializedControllers.updateByDid(req.body.major_name, req.body.major_department_id ,req.body.major_id);
    res.sendResult({}, 202, flag ? "更新成功" : "数据已更新");
  }
);

router.delete(
  '/',
  [
    check('major_id').notEmpty().withMessage('major_id参数不能为空'),
  ],
  async function (req, res, next) {
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data = await specializedControllers.selectById(req.query.major_id);
    if (!data) {
      res.sendResult(null, 400, "删除失败,major_id不存在");
      return;
    }

    let flag = await specializedControllers.deleteByDid(req.query.major_id);
    res.sendResult({}, 203, flag ? "删除成功" : "数据已删除");
  }
);


module.exports = router;
